/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gui.utilities;

import java.awt.Color;
import java.awt.Point;
import java.util.Vector;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 * Manages the xml configuration for maps
 * @author Arvanitis Ioannis
 */
public class MapsXMLManager {
    private XMLProcessor processor;
    private Document doc;
    private Element root;
    private String mapName;
    private Point capacity;
    private Point fragmentation;

    /**
     * Gets the name of map
     * @return the name of map
     */
    public String getMapName() {
        return mapName;
    }

    /**
     * Gets the capacity of map
     * @return the capacity of map
     */
    public Point getCapacity() {
        return capacity;
    }

    /**
     * Sets the capacity of map
     * @param capacity The capacity of map
     */
    public void setCapacity(Point capacity) {
        this.capacity = capacity;
    }

    /**
     * Gets the fragmentation of map
     * @return the fragmentation of map
     */
    public Point getFragmentation() {
        return fragmentation;
    }

    /**
     * Sets the fragmentation of map
     * @param fragmentation The fragmentation of map
     */
    public void setFragmentation(Point fragmentation) {
        this.fragmentation = fragmentation;
    }



    /**
     * Creates new form MapsXMLManager
     * @param mapName The name of map
     */
    public MapsXMLManager(String mapName) {
        processor = new XMLProcessor("Maps");
		doc = processor.readXML();
        root = doc.getDocumentElement();
        this.mapName = mapName;
    }

    /**
     * Saves the given map to xml file
     * @param width Width (in cells) of map
     * @param height Height (in cells) of map
     * @param v Vector with the map details
     */
    public void saveMap(int width, int height, int fragWidth, int fragHeight, Vector<DrawingBean> v) {
        Element mapInfoNode = existedMap(mapName);
        if (mapInfoNode != null) {
            root.removeChild(mapInfoNode);
        }
        mapInfoNode = doc.createElement("map");
        mapInfoNode.setAttribute("name", mapName);
        mapInfoNode.setAttribute("width", ""+width);
        mapInfoNode.setAttribute("height", ""+height);
        mapInfoNode.setAttribute("fragwidth", ""+fragWidth);
        mapInfoNode.setAttribute("fragheight", ""+fragHeight);
        root.appendChild(mapInfoNode);
        for (int i=0; i<v.size(); i++) {
            Element mapTerrainNode = doc.createElement("obstacle");
            mapTerrainNode.setAttribute("row", ""+v.get(i).getPoint().y);
            mapTerrainNode.setAttribute("column", ""+v.get(i).getPoint().x);
            mapTerrainNode.setAttribute("color", v.get(i).getColor().toString());
            mapInfoNode.appendChild(mapTerrainNode);
        }
        processor.writeXML(doc);
    }

    /**
     * Loads the map indicated by the map name
     * @return a vector with the terrain details
     */
    public Vector<DrawingBean> loadMap() {
        Element mapInfoNode = existedMap(mapName);
        if (mapInfoNode == null) {
            return null;
        }
        else {
            Point p = new Point();
            p.x = Integer.parseInt(mapInfoNode.getAttributes().getNamedItem("width").getNodeValue());
            p.y = Integer.parseInt(mapInfoNode.getAttributes().getNamedItem("height").getNodeValue());
            setCapacity(p);
            p.x = Integer.parseInt(mapInfoNode.getAttributes().getNamedItem("fragwidth").getNodeValue());
            p.y = Integer.parseInt(mapInfoNode.getAttributes().getNamedItem("fragheight").getNodeValue());
            setFragmentation(p);
            Vector<DrawingBean> v = new Vector<DrawingBean>();
            NodeList nl = mapInfoNode.getElementsByTagName("obstacle");
            for (int i=0; i<nl.getLength(); i++) {
                p = new Point();
                p.x = Integer.parseInt(nl.item(i).getAttributes().getNamedItem("column").getNodeValue());
                p.y = Integer.parseInt(nl.item(i).getAttributes().getNamedItem("row").getNodeValue());
                String color = nl.item(i).getAttributes().getNamedItem("color").getNodeValue();
                String r = color.substring(color.lastIndexOf("r")+2, color.indexOf(","));
                String g = color.substring(color.lastIndexOf("g")+2, color.lastIndexOf(","));
                String b = color.substring(color.lastIndexOf("b")+2, color.indexOf("]"));
                Color c = new Color(Integer.parseInt(r), Integer.parseInt(g), Integer.parseInt(b));
                v.addElement(new DrawingBean(p, c));
            }
            return v;
        }
    }

    /**
     * Checks the existence of a given map name
     * @param mapname The name of map to check
     * @return the position where map was found or null otherwise
     */
    private Element existedMap(String mapname) {
        NodeList nl = root.getElementsByTagName("map");
        for (int i=0; i<nl.getLength(); i++) {
            if (nl.item(i).getAttributes().getNamedItem("name").getNodeValue().equals(mapname)) {
                return (Element) nl.item(i);
            }
        }
        return null;
    }
}
